﻿using RealTimeCharts.LineChart.common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace RealTimeCharts.LineChart
{
    /// <summary>
    /// X\Y轴标签
    /// </summary>
    public class AxisTitle
    {
        public TitleProperty propX;
        public TitleProperty propY;
        public Padding padding;
        public int pos;

        Graphics gs;
        public Rect myRect;

        public AxisTitle()
        {
            pos = 0; // 无title
            padding = new Padding(2,20,2,2);
        }

        public AxisTitle(string textX,string textY,int pos):this()
        {
            this.pos = pos;
            if((pos & (int)(Position.Left | Position.Right)) != 0)
            {
                propY = new TitleProperty();
                propY.title = textY;
            }else if((pos & (int)(Position.Top | Position.Bottom)) != 0)
            {
                propX = new TitleProperty();
                propX.title = textX;
            }
        }

        public AxisTitle(TitleProperty titleX,TitleProperty titleY, int pos) : this()
        {
            this.pos = pos;
            if ((pos & (int)(Position.Left | Position.Right)) != 0)
            {
                this.propY = titleY.Clone();
            }
            else if ((pos & (int)(Position.Top | Position.Bottom)) != 0)
            {
                this.propY = titleX.Clone();
            }
        }

        public Rect GetRemainRect(Graphics g, Rect r)
        {
            myRect = r;
            Rect paddingRect = r;
            paddingRect.X += padding.left;
            paddingRect.Y += padding.top;
            paddingRect.Width -= (padding.left + padding.right);
            paddingRect.Height -= (padding.top + padding.bottom);

            Rect tmpRect = new Rect(paddingRect.TopLeft, paddingRect.BottomRight);

            if((pos & (int)Position.Left) != 0)
            {
                SizeF titleSize = g.MeasureString(propY.title, propY.titleTextFont);

                tmpRect.X = tmpRect.X + titleSize.Height;
                tmpRect.Width = tmpRect.Width - titleSize.Height;
            }

            if((pos & (int)Position.Right) != 0)
            {
                SizeF titleSize = g.MeasureString(propY.title, propY.titleTextFont);
                tmpRect.Width = tmpRect.Width - titleSize.Height;
            }

            if ((pos & (int)Position.Top) != 0)
            {
                SizeF titleSize = g.MeasureString(propX.title, propX.titleTextFont);
                tmpRect.Y = tmpRect.Y + titleSize.Height;
                tmpRect.Height = tmpRect.Height - titleSize.Height;
            }

            if ((pos & (int)Position.Bottom) != 0)
            {
                SizeF titleSize = g.MeasureString(propX.title, propX.titleTextFont);
                tmpRect.Height = tmpRect.Height - titleSize.Height;
            }

            return tmpRect;
        }

        public Rect Show(Graphics g, Rect r)
        {
            myRect = r;
            Rect paddingRect = r;
            paddingRect.X += padding.left;
            paddingRect.Y += padding.top;
            paddingRect.Width -= (padding.left + padding.right);
            paddingRect.Height -= (padding.top + padding.bottom);

            gs = g;

            Rect tmpRect = new Rect(paddingRect.TopLeft,paddingRect.BottomRight);

            if((pos & (int)Position.Left) != 0)
            {
                SizeF titleSize = g.MeasureString(propY.title, propY.titleTextFont);

                // title的中心点
                PointF center = new PointF();
                center.X = (float)paddingRect.Left  + titleSize.Height / 2;
                center.Y = (float)(paddingRect.Top + paddingRect.Height /2);

                // title的起始点
                PointF startPt = new PointF();
                startPt.X = (float)(center.X - titleSize.Width / 2);
                startPt.Y = (float)(center.Y - titleSize.Height / 2);

                // 以中心点逆时针旋转90度
                Matrix oldMatrix = g.Transform;
                Matrix matrix = new Matrix();
                matrix.RotateAt(-90, center);
                g.Transform = matrix;
                g.DrawString(propY.title, propY.titleTextFont, propY.titleTextBrush, startPt);
                g.Transform = oldMatrix;

                tmpRect.X = tmpRect.X + titleSize.Height;
                tmpRect.Width = tmpRect.Width - titleSize.Height;
            }

            if ((pos & (int)Position.Right) != 0)
            {
                SizeF titleSize = g.MeasureString(propY.title, propY.titleTextFont);

                //title的中心点
                PointF center = new PointF();
                center.X = (float)paddingRect.Right - titleSize.Height / 2;
                center.Y = (float)(paddingRect.Top + paddingRect.Height / 2);

                //title的起始点
                PointF startPt = new PointF();
                startPt.X = (float)(center.X - titleSize.Width / 2); //
                startPt.Y = (float)(center.Y - titleSize.Height / 2);

                //以中心点逆时针旋转90度
                Matrix oldMatrix = g.Transform;
                Matrix matrix = new Matrix();
                matrix.RotateAt(90, center);
                g.Transform = matrix;
                g.DrawString(propY.title, propY.titleTextFont, propY.titleTextBrush, startPt);
                g.Transform = oldMatrix;

                tmpRect.Width = tmpRect.Width - titleSize.Height;
            }

            if ((pos & (int)Position.Top) != 0)
            {
                SizeF titleSize = g.MeasureString(propX.title, propX.titleTextFont);

                //title的起始点
                PointF startPt = new PointF();
                startPt.X = (float)(paddingRect.Left + (paddingRect.Width - titleSize.Width) / 2); //
                startPt.Y = (float)paddingRect.Top;

                g.DrawString(propX.title, propX.titleTextFont, propX.titleTextBrush, startPt);

                tmpRect.Y = tmpRect.Y + titleSize.Height;
                tmpRect.Height = tmpRect.Height - titleSize.Height;
            }

            if ((pos & (int)Position.Bottom) != 0)
            {
                SizeF titleSize = g.MeasureString(propX.title, propX.titleTextFont);

                //title的起始点
                PointF startPt = new PointF();
                startPt.X = (float)(paddingRect.Left + (paddingRect.Width - titleSize.Width) / 2); //
                startPt.Y = (float)paddingRect.Bottom - titleSize.Height;

                g.DrawString(propX.title, propX.titleTextFont, propX.titleTextBrush, startPt);

                tmpRect.Height = tmpRect.Height - titleSize.Height;
            }

            return tmpRect;
        }
    }
}
